#!/usr/bin/env python
"""
创建测试数据的脚本
"""
import os
import django
from decimal import Decimal

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'admin_learn_project.settings')
django.setup()

from core.models import CustomUser
from products.models import Category, Brand, Product
from orders.models import Order, OrderItem

def create_test_data():
    """创建测试数据"""
    print("开始创建测试数据...")

    # 创建测试用户
    users = []
    for i in range(5):
        username = f'user{i+1}'
        if not CustomUser.objects.filter(username=username).exists():
            user = CustomUser.objects.create_user(
                username=username,
                email=f'{username}@example.com',
                password='password123',
                first_name=f'用户{i+1}',
                last_name='测试',
                phone=f'1380000000{i}',
                is_vip=(i % 2 == 0),
                vip_level=(i * 2) if (i % 2 == 0) else 0
            )
            users.append(user)
            print(f"创建用户: {username}")

    # 创建产品分类
    categories = []
    category_data = [
        ('电子产品', '各种电子设备和配件'),
        ('服装鞋帽', '时尚服装和鞋类'),
        ('家居用品', '家庭日用品和装饰'),
        ('图书音像', '书籍、音乐和视频'),
        ('运动户外', '运动器材和户外用品'),
    ]

    for name, desc in category_data:
        category, created = Category.objects.get_or_create(
            name=name,
            defaults={'description': desc, 'is_active': True}
        )
        if created:
            print(f"创建分类: {name}")
        categories.append(category)

    # 创建品牌
    brands = []
    brand_data = [
        ('苹果', 'Apple Inc.'),
        ('华为', 'HUAWEI Technologies'),
        ('小米', 'Xiaomi Corporation'),
        ('耐克', 'Nike Inc.'),
        ('阿迪达斯', 'Adidas AG'),
    ]

    for name, desc in brand_data:
        brand, created = Brand.objects.get_or_create(
            name=name,
            defaults={'description': desc, 'is_featured': True}
        )
        if created:
            print(f"创建品牌: {name}")
        brands.append(brand)

    # 创建产品
    products = []
    product_data = [
        ('iPhone 15 Pro', 'apple-iphone-15-pro', '最新款iPhone，配备钛金属外壳', categories[0], brands[0], Decimal('7999.00'), Decimal('8999.00'), 50),
        ('华为Mate 60', 'huawei-mate-60', '华为旗舰手机，搭载麒麟9000S', categories[0], brands[1], Decimal('5999.00'), Decimal('6999.00'), 30),
        ('小米14 Ultra', 'xiaomi-14-ultra', '小米影像旗舰，徕卡认证', categories[0], brands[2], Decimal('4999.00'), Decimal('5999.00'), 40),
        ('Nike Air Jordan', 'nike-air-jordan', '经典篮球鞋，限量版', categories[1], brands[3], Decimal('1299.00'), Decimal('1599.00'), 20),
        ('阿迪达斯三叶草', 'adidas-trefoil', '经典运动鞋，复古设计', categories[1], brands[4], Decimal('899.00'), Decimal('1199.00'), 25),
    ]

    for name, slug, desc, category, brand, price, orig_price, stock in product_data:
        product, created = Product.objects.get_or_create(
            slug=slug,
            defaults={
                'name': name,
                'description': desc,
                'short_description': desc[:50],
                'category': category,
                'brand': brand,
                'price': price,
                'original_price': orig_price,
                'stock_quantity': stock,
                'status': 'active',
                'is_featured': True
            }
        )
        if created:
            print(f"创建产品: {name}")
        products.append(product)

    print(f"测试数据创建完成！")
    print(f"创建了 {len(categories)} 个分类")
    print(f"创建了 {len(brands)} 个品牌")
    print(f"创建了 {len(products)} 个产品")

if __name__ == '__main__':
    create_test_data()
